using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Castle.MonoRail.Framework;
using Castle.MonoRail.ActiveRecordSupport;
using Saffron.Filters;
using Saffron.Framework.Projects;
using Saffron.Framework.Users;

namespace Saffron.Controllers
{
    [Layout("default"), Rescue("generalerror")]
    [Filter(ExecuteEnum.BeforeAction, typeof(AuthenticationFilter))]
    public class ProjectController : PublicBaseController
    {

        public void Index()
        {

        }

        public void Create()
        {
            PropertyBag["ProjectVisibility"] = GetProjectVisibilityLookup();
            PropertyBag["ProjectApplicationProcess"] = GetApplicationProcessLookup();
        }

        public void CreateProject([ARDataBind("Project")] Project project)
        {
            project.CreatedDate = DateTime.Now;
            project.CreatedBy = (UserAccount) Session["user"];
            project.Create();

            NameValueCollection param = new NameValueCollection();
            param.Add("projectId", project.Id.ToString());

            Redirect("project", "View", param);
         
        }

        public void View([ARFetch("projectId", false, true)] Project project)
        {
            PropertyBag["Project"] = project;
            PropertyBag["ProjectVisibility"] = GetProjectVisibilityLookup();
            PropertyBag["ProjectApplicationProcess"] = GetApplicationProcessLookup();

            if (!project.UserCanViewProject(CurrentUser))
            {
                RenderView("Denied");
                return;
            }

            
        }

        public void Find()
        {

        }

        public void Search()
        {

        }

        public void Denied()
        {

        }

        private Dictionary<ProjectVisibility, string> GetProjectVisibilityLookup()
        {
            Dictionary<ProjectVisibility, string> result = new Dictionary<ProjectVisibility, string>();
            result.Add(ProjectVisibility.Private, "Private");
            result.Add(ProjectVisibility.Public, "Public");
            return result;
        }

        private Dictionary<ApplicationProcess, string> GetApplicationProcessLookup()
        {
            Dictionary<ApplicationProcess, string> result = new Dictionary<ApplicationProcess, string>();
            result.Add(ApplicationProcess.OpenToAll, "Open to everyone");
            result.Add(ApplicationProcess.Application, "Apply to join");
            result.Add(ApplicationProcess.InviteOnly, "Invitation only");
            return result;
        }
    }
}
